package com.jinke.api.modules.app.util;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @author huang
 * @data 2024-06-04
 */
public class JavaMailUntil {
    private JavaMailUntil() {}


    /**
     * 阿里服务器不让25端口的发邮件
     * @return
     */
    @Deprecated
    public static Session createSession() {

        //	账号信息
        //	邮箱发送账号
        String username = "15364494269@163.com";
        //	邮箱授权码
        String password = "FGTFEJPVIYQTKBYU";

        //	创建一个配置文件，并保存
        Properties props = new Properties();

        props.put("mail.smtp.host", "smtp.163.com");
        props.put("mail.smtp.port", "25");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable","true" );

        Session session = Session.getInstance(props,new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // TODO Auto-generated method stub
                return new PasswordAuthentication(username,password);
            }
        });
        //  控制台打印调试信息
        session.setDebug(true);
        return session;
    }
    public static Session createSessionForQQ() {

        //	账号信息
        //	邮箱发送账号
        String username = "husuruo@qq.com";
        //	邮箱授权码
        String password = "vxskkcsvloizbbid";

        //	创建一个配置文件，并保存
        Properties props = new Properties();

        props.put("mail.smtp.host", "smtp.qq.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.port", "587");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        Session session = Session.getInstance(props,new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // TODO Auto-generated method stub
                return new PasswordAuthentication(username,password);
            }
        });
        //  控制台打印调试信息
        //session.setDebug(true);
        return session;
    }
//    public static Session createSession() {
//
//        // 账号信息
//        // 邮箱发送账号
//        String username = "ship-cheap@aliyun.com";
//        // 邮箱密码
//        String password = "sr@ship-cheap";
//
//        // 创建一个配置文件，并保存
//        Properties props = new Properties();
//
//        props.put("mail.smtp.host", "smtp.mxhichina.com");
//        props.put("mail.smtp.port", "465");
//        props.put("mail.smtp.auth", "true");
//        props.put("mail.smtp.socketFactory.port", "465");
//        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//
//        Session session = Session.getInstance(props, new Authenticator() {
//            @Override
//            protected PasswordAuthentication getPasswordAuthentication() {
//                // TODO Auto-generated method stub
//                return new PasswordAuthentication(username, password);
//            }
//        });
//        //  控制台打印调试信息
//        session.setDebug(true);
//        return session;
//    }


    /**
     * 阿里邮箱没调通
     * @return
     */
    @Deprecated
    public static Session createSessionForAliyun() {
        // 账号信息
        String username = "ship-cheap@aliyun.com";
        String password = "sr@ship-cheap";

        // 创建一个配置文件，并保存
        Properties props = new Properties();

        String value ="smtp.qiye.aliyun.com";
                //"smtp.mxhichina.com";
        props.put("mail.smtp.host", value);
        props.put("mail.smtp.port", "465");  // 使用 ssl 加密端口 465
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        session.setDebug(true);
        return session;
    }

    public static void main(String[] args) {
        //1.创建Session
        try {
            Session session = JavaMailUntil.createSessionForQQ();

            //2.创建邮件对象
            MimeMessage message=new MimeMessage(session);
            //设置邮件主题
//            message.setSubject("这是一个测试邮件");
            //设置邮件内容
            message.setText("测试邮件");
            //设置发件人
//            message.setFrom(new InternetAddress("ship-cheap@qq.com"));

            //设置发件人
            message.setFrom(new InternetAddress("husuruo@qq.com"));


            //设置收件人
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("husuruo@ship-cheap.com"));
            //设置抄送人
//            message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress("husuruo@qq.com"));
            //3.发送邮件
            Transport.send(message);
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
